package com.sky.service.impl;

import com.sky.Entity.AddressBook;
import com.sky.Entity.OrderDetail;
import com.sky.Entity.Orders;
import com.sky.Entity.ShoppingCart;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.exception.AddressBookEmptyException;
import com.sky.exception.ShoppingCartEmptyException;
import com.sky.mapper.AddressBookMapper;
import com.sky.mapper.OrderDetailMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private AddressBookMapper addressBookMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private OrderDetailMapper detailMapper;

    @Override
    @Transactional
    public OrderSubmitVO subimitOrder(OrdersSubmitDTO ordersSubmitDTO) {

        Orders orders=new Orders();
        BeanUtils.copyProperties(ordersSubmitDTO,orders);

        //判断地址是否为空
        AddressBook addressBook = addressBookMapper.queryById(orders.getAddressBookId());
        if (addressBook == null) {
            throw new AddressBookEmptyException(MessageConstant.ADDRESS_BOOK_IS_NULL);
        }

        //判断购物车是否为空
        List<ShoppingCart> list = shoppingCartMapper.queryByUserId(BaseContext.getEmpId());
        if(list==null||list.size()==0){
            throw new ShoppingCartEmptyException(MessageConstant.SHOPPING_CART_IS_NULL);
        }

        //填充数据
        orders.setNumber(String.valueOf(System.currentTimeMillis()));
        orders.setStatus(Orders.PENDING_PAYMENT);
        orders.setUserId(BaseContext.getEmpId());
        orders.setOrderTime(LocalDateTime.now());

        //计算订单金额
//        BigDecimal amount=new BigDecimal(0);
//        for (ShoppingCart cart : list) {
//            amount=amount.add(cart.getAmount());
//        }
//        orders.setAmount(amount);
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        orders.setPayStatus(Orders.UN_PAID);
        orders.setConsignee(addressBook.getConsignee());

        orderMapper.insert(orders);

        List<OrderDetail> orderDetailList=new ArrayList<>();
        for (ShoppingCart cart : list) {
            OrderDetail orderDetail=new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setOrderId(orders.getId());
            orderDetailList.add(orderDetail);
        }
        detailMapper.insertList(orderDetailList);

        shoppingCartMapper.deleteByUserId(BaseContext.getEmpId());


        OrderSubmitVO vo=new OrderSubmitVO();
        vo.setOrderAmount(orders.getAmount());
        vo.setOrderNumber(orders.getNumber());
        vo.setOrderTime(orders.getOrderTime());

        return vo;
    }

    @Override
    public OrderPaymentVO payment(OrdersPaymentDTO ordersPaymentDTO) {

        return null;
    }
}
